package me.leon.mylibs.widget;

import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.Color;
import android.support.v4.content.ContextCompat;
import android.view.View;

import com.alibaba.fastjson.JSON;
import com.bigkoo.pickerview.OptionsPickerView;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import me.leon.mylibs.DB.JsonBean;


/**
 * @author PC
 * @date 2017/12/20
 * @desc 获取三级联动数据
 */

public class CityPickerHelper {

    private List<JsonBean> options1Items = new ArrayList<>();
    private List<List<String>> options2Items = new ArrayList<>();
    private List<List<List<String>>> options3Items = new ArrayList<>();
    private String defStr = "不限";
    private Context context;
    private OnCityPickerListener listerner;

    public CityPickerHelper(Context ctx, boolean limit) {
        context = ctx;
        String JsonData = getJson(ctx, "province.json");
        options1Items = JSON.parseArray(JsonData, JsonBean.class);

        for (int i = 0; i < options1Items.size(); i++) {
            List<String> CityList = new ArrayList<>();
            List<List<String>> Province_AreaList = new ArrayList<>();

            for (int c = 0; c < options1Items.get(i).city.size(); c++) {
                String CityName = options1Items.get(i).city.get(c).name;
                CityList.add(CityName);

                List<String> City_AreaList = new ArrayList<>();

                //如果无地区数据，建议添加空字符串，防止数据为null 导致三个选项长度不匹配造成崩溃
                if (options1Items.get(i).city.get(c).area == null
                        || options1Items.get(i).city.get(c).area.size() == 0) {
                    City_AreaList.add("");
                } else {

                    for (int d = 0; d < options1Items.get(i).city.get(c).area.size(); d++) {
                        String AreaName = options1Items.get(i).city.get(c).area.get(d);

                        City_AreaList.add(AreaName);
                    }
                    if (limit) {  //三级
                        City_AreaList.add(0, defStr);
                    }

                }
                Province_AreaList.add(City_AreaList);

            }
            //添加字段
            if (limit) {  //二级添加 + 关联
                CityList.add(0, defStr);
                List<String> temp = new ArrayList<>();
                temp.add(defStr);
                Province_AreaList.add(0, temp);
            }

            options2Items.add(CityList);
            options3Items.add(Province_AreaList);
        }
    }

    public CityPickerHelper(Context ctx) {
        this(ctx, false);
    }

    private String getJson(Context context, String fileName) {

        StringBuilder stringBuilder = new StringBuilder();
        try {
            AssetManager assetManager = context.getAssets();
            BufferedReader bf = new BufferedReader(new InputStreamReader(
                    assetManager.open(fileName)));
            String line;
            while ((line = bf.readLine()) != null) {
                stringBuilder.append(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return stringBuilder.toString();
    }

    public List<JsonBean> getOptions1Items() {
        return options1Items;
    }

    public List<List<String>> getOptions2Items() {
        return options2Items;
    }

    public List<List<List<String>>> getOptions3Items() {
        return options3Items;
    }

    public CityPickerHelper setListener(OnCityPickerListener listener) {
        listerner = listener;
        return this;
    }

    public void show() {
        if (listerner == null) {
            throw (new IllegalStateException("Listener is not initialized"));
        }

        OptionsPickerView pickerView = new OptionsPickerView
                .Builder(context, (i1, i2, i3, v) -> {
            String province = getOptions1Items().get(i1).getPickerViewText();
            String city = getOptions2Items().get(i1).get(i2);
            String district = getOptions3Items().get(i1).get(i2).get(i3);
            listerner.onPicker(province, city, district, v);
        })
                .setCancelColor(Color.RED)
                .setSubCalSize(14)
                .setContentTextSize(14)
                .setLineSpacingMultiplier(4.5f)
                .setLabels(null, null, null)
                .build();
        pickerView.setPicker(options1Items, options2Items, options3Items);
        pickerView.show();
    }

    public interface OnCityPickerListener {
        void onPicker(String province, String city, String district, View v);
    }
}
